var countdown=59;//短信倒计时时间
var phone_no = "";
var verifySignCode = false;//短信验证码校验结果
$(function () {
    $("#submit").on("click", function () {
        amalgamate_submit();
    });

    $("#cancel").on("click", function () {
        window.history.go(-1);
    });
});

/*
发送验证码
*/
var sendSmsCode = function(tel) {
  console.log("sendSmsCode:" + tel)
  var url = Constants.apiV2Host + "sms/send?phoneNumber=" + tel + "&smsType=9&empCode=" + getParamValue("EmpCode");
  console.log('url:' + url);
  $.ajax({
    type: "get",
    url: url,
    contentType: "application/json",
    dataType: "json",
    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }      
      if (data.ResultCode == 1000) {        
        count_down();
        alert("发送验证码成功！");
      } else {
        alert("发送验证码失败！"+data.Message);        
      }
    },
    error: function(textStatus) {
      alert("发送验证码失败！");
    }
  });
}

/**
 * 获取短信验证码
 */
var get_msg_code = function(){
    phone_no = $("#phone_master").val().trim();
    if(!phone_no || phone_no.length < 11){
        alert("请输入主卡号码");
        return false;
    }
    if(!Constants.cmccMobileReg.test(phone_no)){
        alert('请输入有效移动的主卡号码！');
        return false;
    }
    sendSmsCode(phone_no);
};


/**
 * 获取短信验证码倒计时
 */
function count_down() {
    if (countdown == 0) {
        $("#get_msg_code").removeAttr("disabled");
        $("#get_msg_code").html("获取验证码");
        countdown = 59;
        return false;
    } else {
        $("#get_msg_code").attr("disabled","disabled");
        $("#get_msg_code").html("重新发送(" + countdown + ")");
        countdown--;
    }
    setTimeout(function() {
        count_down()
    },1000)
}



/**
 * 验证短信验证码是否合法
 */
var vali_msg_code = function(){
    var msg_code = $("#msg_code").val().trim();
    if(!msg_code || msg_code.length < 4){
        alert("请输入短信验证码");
        return false;
    }
    phone_no = $("#phone_master").val().trim();
    if(!phone_no || phone_no.length < 11){
        alert("请输入主卡号码");
        return false;
    }

    if(!Constants.cmccMobileReg.test(phone_no)){
        alert('请输入有效移动的主卡号码！');
        return false;
    }
    verifySms(phone_no, msg_code);
};

var verifySms = function(tel, code) {
  var url = Constants.apiV2Host + "sms/validate?phoneNumber=" + tel + "&smsCode=" + code + "&smsType=9&empCode=" + getParamValue("EmpCode");
  console.log('url:' + url);
  $.ajax({
    type: "get",
    url: url,
    contentType: "application/json",
    dataType: "json",
    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
      console.info(data);
      if (data && data.ResultCode == 1000) {
        alert("验证成功");
        verifySignCode = true;
      }else{
        alert("短信验证码校验失败！" + data.Message);
      }
    },
    error: function(textStatus) {
      alert("短信验证码校验失败！");
    }
  });
};

var amalgamate_submit = function(){
    var phone_master = $("#phone_master").val().trim();
    var phone_slave = $("#phone_slave").val().trim();
    var phone_slave_second = $("#phone_slave_second").val().trim();

    if(!phone_master){
        alert("主卡号码不能为空");
        return false;
    }

    if(phone_master != phone_no || !verifySignCode){
        alert("请先填写短信验证码并点击验证按钮");
        return false;
    }
    if(!phone_slave || !phone_slave_second){
        alert("副卡号码不能为空");
        return false;
    }
    if(!Constants.cmccMobileReg.test(phone_slave)){
        alert('请输入有效移动的副卡号码！');
        return false;
    }
    if(phone_slave != phone_slave_second){
        alert("两次输入的副卡号码不一致");
        return false;
    }
    if(phone_slave == phone_master){
        alert("主卡号码和副卡号码不能相同");
        return false;
    }

    var url = Constants.apiV2HostRest + "s1627BandL";
    var data = {
        "BODY": {
            "MID_NO": "",
            "PHONE_NO": getParamValue("phonenumber"),
            "MPHONE_NO": phone_master,
            "SPHONE_NO": phone_slave,
            "OP_TYPE": "I",
            "OP_CODE": "1627",
            "LOGIN_NO": getParamValue("EmpCode"),
            "CHK_FLAG": "S",
            "KEY_CONTRACT_NO": "0",
            "OP_NOTE": ""
        }
    };
    console.log("esb.s1627BandL.url=" + url);
    console.log("esb.s1627BandL.data=" + JSON.stringify(data));
    $.ajax({
      type: "post",
      url: url,
      data: JSON.stringify(data),
      contentType: "application/json",
      dataType: "json",
      headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
        console.log("data:" + JSON.stringify(data));
        if(data.ROOT.RETURN_CODE == 0){
          alert("合账办理成功");
          window.history.go(-1);
        } else {            
          var message = data.ROOT.RETURN_MSG;
          if (typeof (data.ROOT.DETAIL_MSG) != 'undefined'){
            message = data.ROOT.DETAIL_MSG;
          }
          alert(message);
          $("#submit").removeAttr("disabled");
          $("#submit").html("办理合账");
        }
      },
      error: function(textStatus) {
        alert("网络或服务器异常，请稍后再试！");
        $("#submit").removeAttr("disabled");
        $("#submit").html("办理合账");
      }
    });
};


